<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\User as userModel;
class User extends Common{
    //用户模型
    protected $user;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->user = new userModel();
    }
    /**
     *用户列表
     * return json
     */
    public function userList(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $status = $this->request->post('status',0); //1:正常 2:冻结
        $keywords = $this->request->post('keywords');
        $pageNo = $this->request->post('pageNo');
        $pageSize = $this->request->post('pageSize');
        $where=[];
       if($status==1){
           $where['status']=1;
       }elseif ($status==2){
           $where['status']=0;
       }
       if(!empty($keywords)){
           $where['phone']=['like',"%$keywords%"];
       }
       $list = $this->user->where($where)->page($pageNo,$pageSize)->order('uid','desc')->select();
       $total = $this->user->where($where)->count();
       if(!empty($list)){
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['list']=$list;
           $msg['total']=$total;
       }else{
           $msg['message']=checkMsg(GET_DATA_FAILE);
       }
       return $msg;
    }
    /**
     *修改用户的状态
     * return json
     */
    public function editeStatus(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $user_id = $this->request->post('user_id');
        $status  = $this->request->post('status',0); //1:解冻 0:冻结
        if(empty($user_id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->user->where(['uid'=>$user_id])->count();
        if($count<1){
            $msg['message']=checkMsg(USER_NOT_EXISTS);
            return $msg;
        }
        $res = $this->user->save(['status'=>$status],['uid'=>$user_id]);
        if($res){
            $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
        }else{
            $msg['message']=checkMsg(UPDATE_DATA_FAIL);
        }
        return $msg;
    }
    /**
     *删除用户
     * return json
     */
    public function deleteUser(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $user_id = $this->request->post('user_id');
        $count = $this->user->where(['uid'=>$user_id])->count();
        if($count<1){
            $msg['message']=checkMsg(USER_NOT_EXISTS);
            return $msg;
        }
        $res = $this->user->where(['uid'=>$user_id])->delete();
        if($res){
            $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
        }else{
            $msg['message']=checkMsg(DELETE_DATA_FAIL);
        }
        return $msg;
    }
}